package com.lgh.hotel.config;

import com.lgh.hotel.handler.security.FailureHandler;
import com.lgh.hotel.handler.security.HandlerAuthenticationFilter;
import com.lgh.hotel.handler.security.SuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.firewall.HttpFirewall;
import org.springframework.security.web.firewall.StrictHttpFirewall;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //White list
    private final static String[] WHITE_LIST = {
            "/doc.html","/webjars/**","/img.icons/**","/swagger-resources/**",
            "/v2/api-docs","/yiqing","/guest/**","/table/**",
            "/table","/table/addTable","/roomType","/temp","/temp/**","/server/**","/punch/**","/statistics","/statistics/**"
    };


    //不能这样注入 因为还要把原来的auth放入才行
//    @Autowired
//    HandlerAuthenticationFilter handlerAuthenticationFilter;

    @Bean
    SuccessHandler successHandler(){
        SuccessHandler successHandler = new SuccessHandler(redisTemplate);
        return successHandler;
    }

    @Autowired
    FailureHandler failureHandler;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @Bean
    HandlerAuthenticationFilter handlerAuthenticationFilter() throws Exception {
        //new一个自写handler处理器 并把原来真正的处理器放进去（才可以用）
        return new HandlerAuthenticationFilter(authenticationManager(),redisTemplate);
    }

//    @Bean
//    public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
//        StrictHttpFirewall firewall = new StrictHttpFirewall();
//        firewall.setAllowSemicolon(true);
//        return firewall;
//    }


    @Bean
    BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }




//    @Override
//    public void configure(WebSecurity web) throws Exception {
//        super.configure(web);
//        web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
//    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        //关闭-防火墙-伪造
       http.cors().and().csrf().disable();

        http.formLogin()
                .loginProcessingUrl("/sysUser/login")
                .successHandler(successHandler())
                .failureHandler(failureHandler)
                //禁止原来的session 使用自己的jwt
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)

                .and()
                .addFilter(handlerAuthenticationFilter());

        http.authorizeRequests()// 对请求授权
                .antMatchers(WHITE_LIST).permitAll() //白名单
                .anyRequest() //
                .authenticated()//  需要身份认证
        ;

    }
}
